﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using StockTrader.Core;
using StockTrader.Platform.Logging;
using StockTrader.Stocks.Utilities.Broker;
using StockTrader.Utilities;

namespace Experiments
{
    partial class Program
    {
        static void CleanseTickFiles()
        {
            try
            {
                TickFormatType tickFormat = TickFormatType.OnlyLTP;
                string inputStocksDir = @"C:\StockTest\Indices";
                string outputStocksDir = @"C:\StockTest\Cleaned";

                IEnumerable<string> stockPaths = Directory.EnumerateFiles(inputStocksDir, "*");//, SearchOption.AllDirectories);

                SortedList<string, string> sortedStockPaths = new SortedList<string, string>();
                foreach (var p in stockPaths)
                    sortedStockPaths.Add(Path.GetFileName(p), p);

                foreach (string filePath in sortedStockPaths.Values)
                {
                    SortedList<DateTime, double> ticks = new SortedList<DateTime, double>();
                    SortedList<DateTime, double> ticksDay = new SortedList<DateTime, double>();
                    var arr = File.ReadAllLines(filePath);
                    double ltp = 0;
                    int tickCount = 0;
                    if (arr.Length < 1) continue;
                    var dqi = AlgoRunUtils.ParseTickString(arr[0], tickFormat);
                    DateTime DayToday = dqi.QuoteTime;
                    double lastPrice = dqi.LastTradedPriceDouble;
                    bool skipDay = false;
                    foreach (var s in arr)
                    {
                        dqi = AlgoRunUtils.ParseTickString(s, tickFormat);
                        bool isNextDay = (dqi.QuoteTime.Date != DayToday.Date);

                        if (isNextDay)
                        {
                            DayToday = dqi.QuoteTime;
                            if (!skipDay && ticksDay.Count > 1000)
                            {
                                var time = ticksDay.Keys[ticksDay.Count - 1];
                                if (time.Hour >= 15)
                                {
                                    // valid set, add it to main ticks list
                                    foreach (var tick in ticksDay)
                                        ticks.Add(tick.Key, tick.Value);
                                }
                                else tickCount++;
                            }
                            else tickCount++;

                            skipDay = false;
                            ticksDay.Clear();
                        }
                        else
                        {
                            var percDiff = (100 * Math.Abs(lastPrice - dqi.LastTradedPriceDouble)) / dqi.LastTradedPriceDouble;
                            if (percDiff > 0.5)
                            {
                                skipDay = true;
                            }
                        }


                        try
                        {
                            if (!ticksDay.ContainsKey(dqi.QuoteTime))
                                //dqi.QuoteTime = dqi.QuoteTime.AddSeconds(1);

                                ticksDay.Add(dqi.QuoteTime, dqi.LastTradedPriceDouble);

                            else ltp += 0.10;
                        }
                        catch
                        {
                            //ticksDay.Add(dqi.QuoteTime.AddSeconds(1), dqi.LastTradedPriceDouble);
                        }

                        lastPrice = dqi.LastTradedPriceDouble;
                    }

                    // write the ticks to file
                    var filename = Path.GetFileName(filePath);
                    string newFilePath = Path.Combine(outputStocksDir, filename);
                    using (FileStream fs = new FileStream(newFilePath, FileMode.Create))
                    using (StreamWriter sw = new StreamWriter(fs))
                        foreach (var tick in ticks)
                        {
                            sw.WriteLine(tick.Key.ToString("yyyyMMdd:HH:mm:ss") + "," + tick.Value.ToString());
                        }


                    GC.Collect();
                }
            }
            catch { }
        }

        static void CollatePeriodPartTickFiles()
        {
            string stocksDir = @"C:\StockRunFiles\TickFiles\";
            string outStocksDir = @"C:\StockRunFiles\All\";

            IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDir, "*", SearchOption.AllDirectories);

            SortedList<DateTime, string> sortedStockPaths = new SortedList<DateTime, string>();
            int i = 1;
            foreach (var p in stockPaths)
            {
                FileInfo fi = new FileInfo(p);
                var time = fi.LastWriteTime;
                if (sortedStockPaths.ContainsKey(time))
                {
                    i += 60;
                    time = time.AddSeconds(i);
                }
                sortedStockPaths.Add(time, p);
            }

            foreach (string filePath in sortedStockPaths.Values)
            {
                //var splitPath = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar);

                string origStockName = Path.GetFileName(filePath);
                string stockName = origStockName.Remove(origStockName.IndexOf('.')); // Remove extension

                //origStockName = stockName.Remove(stockName.LastIndexOf("-"));

                if (!stockName.Contains("OPT-"))
                {
                    if (stockName.Contains("FUT-"))
                        stockName = stockName.Replace("FUT-", "");
                    if (stockName.Contains('-'))
                        stockName = stockName.Remove(stockName.IndexOf('-')); // just get the symbol for collected mock tick files
                    if (stockName.Contains('_'))
                        stockName = stockName.Remove(stockName.IndexOf('_')); // just get the symbol for collected mock tick files
                }

                File.AppendAllText(Path.Combine(outStocksDir, stockName + ".txt"), File.ReadAllText(filePath));
                //File.Delete(filePath);
            }

        }

        static void CollateDayPartTickFiles(int days = 0)
        {
            DateTime allowTicksOnOrAfterDate = DateTime.Now.AddDays(days);

            string stocksDir = @"C:\StockRunFiles\TickFiles\";
            string outDir = Path.Combine(SystemUtils.GetStockRunTempLocation(), "TickFiles");

            IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDir, "*", SearchOption.AllDirectories);

            SortedList<string, string> sortedStockPaths = new SortedList<string, string>();
            foreach (var p in stockPaths)
            {
                var splitPath = Path.GetDirectoryName(p).Split(Path.DirectorySeparatorChar);
                var r1 = splitPath[splitPath.Length - 1];

                var fileDateStr = r1.Insert(2, "-").Insert(5, "-");
                DateTime fileDate = DateTime.Parse(fileDateStr);

                if (fileDate < allowTicksOnOrAfterDate)
                    continue;

                if (!MarketUtils.IsTimeAfter330Market() && fileDate.Date == DateTime.Now.Date) continue;

                var outDateDir = Path.Combine(outDir, r1);

                if (!Directory.Exists(outDateDir))
                    Directory.CreateDirectory(outDateDir);

                sortedStockPaths.Add(r1+Path.GetFileName(p), p);
                File.Copy(p, Path.Combine(outDateDir, Path.GetFileName(p)), true);
            }

            Dictionary<string, string> fileTime = new Dictionary<string, string>();
            foreach (string filePath in sortedStockPaths.Values)
            {
                var splitPath = Path.GetDirectoryName(filePath).Split(Path.DirectorySeparatorChar);
                var r1 = splitPath[splitPath.Length - 1];
                var fileDateStr = r1.Insert(2, "-").Insert(5, "-");
                DateTime fileDate = DateTime.Parse(fileDateStr);

                string stockName = Path.GetFileName(filePath);
                string origStockName = stockName.Remove(stockName.IndexOf('.')); // Remove extension

                origStockName = stockName.Remove(stockName.LastIndexOf("-")); // Remove time part

                var ticks = File.ReadAllLines(filePath);

                if (ticks.Length > 0)
                {
                    DateTime dt = DateTime.MinValue;
                    int i = 0;
                    do
                    {
                        IFormatProvider culture = new CultureInfo("en-US", true);
                        dt = DateTime.ParseExact(ticks[i++].Split(';')[0], "yyyyMMdd:HH:mm:ss", culture);
                    } while (i < ticks.Length && dt.Date < fileDate);

                    if (!fileTime.ContainsKey(r1 + origStockName))
                        fileTime.Add(r1 + origStockName, origStockName + "-" + dt.ToString("HHmm") + ".txt");

                    var fileName = fileTime[r1 + origStockName];
                    var ticks1 = new string[ticks.Length - i + 2];
                    //string contents =  File.ReadAllText(filePath); 
                    Array.Copy(ticks, i - 1, ticks1, 0, ticks.Length - i + 1);
                    File.Delete(filePath);
                    ticks1[ticks.Length] = "";
                    File.AppendAllText(Path.Combine(Path.GetDirectoryName(filePath), fileName), string.Join("\n", ticks1));
                }
                else File.Delete(filePath);
            }
        }

        static void Expr1()
        {
            string stocksDirOrig = @"H:\StockData\1Parshant-Global-data\1-minute-indices-unzip\BigFiles";
            string stocksDirNew = @"H:\StockData\1Parshant-Global-data\1-minute-indices-myformat";

            string stocksDir = @"C:\StockRunFiles\TickFiles\230412";

            if (!Directory.Exists(stocksDirNew))
                Directory.CreateDirectory(stocksDirNew);

            IEnumerable<string> stockPaths = Directory.EnumerateFiles(stocksDirOrig, "*");//, SearchOption.AllDirectories);

            string symbol1 = "";

            foreach (string filePath in stockPaths)
            {
                string stockName = Path.GetFileName(filePath);
                string newFile = Path.Combine(stocksDirNew, stockName);
                var fs = File.Create(newFile);
                fs.Close();
                fs.Dispose();

                using (var sr = new StreamReader(filePath))
                using (var sw = new StreamWriter(newFile))
                {
                    string tick = sr.ReadLine();
                    do
                    {
                        try
                        {
                            tick = sr.ReadLine();

                            string[] tickRecord = tick.Split(',');

                            string tempStr = tickRecord[0].Substring(3, 3);
                            tickRecord[0] = tickRecord[0].Remove(3, 3);
                            tempStr = tickRecord[0].Insert(0, tempStr);

                            DateTime lastTradeTime = DateTime.Parse(tempStr);
                            tempStr = tickRecord[1].Insert(2, ":");

                            //int time = int.Parse(tempStr);

                            lastTradeTime += TimeSpan.Parse(tempStr);

                            double ltp = double.Parse(tickRecord[4]);
                            string str = lastTradeTime.ToString("yyyyMMdd:HH:mm:ss") + "," + ltp;
                            sw.WriteLine(str);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                    } while (!sr.EndOfStream);
                }
            }
        }

        static void Expr2()
        {
            int[] arr = new int[10000];
            //return;


            string fileName = @"H:\StockData\1Parshant-Global-data\1-minute-indices-unzip";
            string tickFileName = @"D:\StockData\NIFTY_ticks.txt";
            //FileStream fs = new FileStream(tickFileName, FileMode.Create, FileAccess.Write);
            //FileInfo fi = new FileInfo(fileName);

            //FileStream fs = fi.OpenRead();

            //NIFTY xy_F1,20091231,14:20,5205.70,5206.50,5204.00,5206.50,127350
            //20090915:09:56:14;260.55;260.4;260.7;2500;1250;0

            StreamReader sr = new StreamReader(fileName);
            StreamWriter sw = new StreamWriter(tickFileName);
            int tickNumber = 0;

            do
            {
                //tickNumber++;

                string tick = sr.ReadLine();

                string[] tickRecord = tick.Split(',');

                string tempStr = tickRecord[1].Insert(4, "-").Insert(7, "-");
                DateTime lastTradeTime = DateTime.Parse(tempStr);
                tempStr = tickRecord[2];

                lastTradeTime += TimeSpan.Parse(tempStr);

                double ltp = double.Parse(tickRecord[4]);
                double bid = double.Parse(tickRecord[5]);
                double offer = double.Parse(tickRecord[6]);

                // default 1000 offer and bid qty and volume traded as 0

                //Debug.Assert

                DerivativeSymbolQuoteRecord dsqr = new DerivativeSymbolQuoteRecord(new DerivativeSymbolQuote(ltp, bid, offer, 1000, 1000));

                //using (StreamWriter sw = new StreamWriter(fs))
                {
                    //DateTime Now = DateTime.Now;
                    string str = lastTradeTime.ToString("yyyyMMdd:HH:mm:ss") + ";" + dsqr.LastTradePrice + ";" + dsqr.BidPrice +
                        ";" + dsqr.OfferPrice + ";" + dsqr.BidQty + ";" + dsqr.OfferQty + ";" + 1000;
                    sw.WriteLine(str);
                }

                // Insert in DB
                //SqlConnection conn = new SqlConnection();
                //SqlCommand cmd = new SqlCommand("dd", conn);


            } while (!sr.EndOfStream);// && tickNumber < 0);
        }
    }
}
